﻿using System;
using System.Collections.Generic;
using System.IO;
using SQLite;
using Bilibili.Common.Database.Table;
using PCLUntils.Objects;
using PCLUntils.Plantform;
using PCLUntils;

namespace Bilibili.Common.Database
{
    /// <summary>
    /// 数据库操作类
    /// </summary>
    public sealed class DB : IDisposable
    {
        public SQLiteConnection Connection { get; }
        private static string _DbPath;
        private static string DbPath
        {
            get
            {
                if (_DbPath.IsEmpty())
                {
                    if (PlantformUntils.System == Platforms.Android)
                        _DbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "data.db");
                    else
                        _DbPath = Path.Combine(Extension.GetRoot(true), "data.db");
                }
                return _DbPath;
            }
        }
        public DB()
        {
            Connection = new SQLiteConnection(DbPath);
            if (!HasTable())
            {
                Connection.CreateTable<HistoryTab>(CreateFlags.None);
                Connection.CreateTable<Setting>(CreateFlags.AutoIncPK);
                Connection.CreateTable<CacheDataTab>(CreateFlags.None);
                Connection.CreateTable<DownloadGuid>(CreateFlags.None);
                Connection.CreateTable<ImageCacheTab>(CreateFlags.AutoIncPK);
                Connection.CreateTable<DanmakuFilterTab>(CreateFlags.AutoIncPK);
                try
                {
                    Connection.InsertAll(new List<Setting>
                    {
                        new Setting { Name = "UseDASH", Value = "1" },
                        new Setting { Name = "IsIJKPlayer", Value = "0" },
                        new Setting { Name = "FingerAuth", Value = "0" },
                        new Setting { Name = "EnableLogging", Value = "1" },
                        new Setting { Name = "DanmakuStatus", Value = "1" },
                        new Setting { Name = "FullScreenPlayer", Value = "0" }
                    });
                    if (PlantformUntils.System == Platforms.Windows && PlantformUntils.IsArmArchitecture)
                        Connection.Insert(new Setting { Name = "IsVlclayer", Value = "0" });
                    else
                        Connection.Insert(new Setting { Name = "IsVlclayer", Value = "1" });
                }
                catch { }
            }
        }
        /// <summary>
        /// 判断是否存在表
        /// </summary>
        /// <returns></returns>
        private bool HasTable()
        {
            try
            {
                Connection.Table<Setting>().FirstOrDefault();
                Connection.Table<DownloadGuid>().FirstOrDefault();
                Connection.Table<HistoryTab>().FirstOrDefault();
                Connection.Table<CacheDataTab>().FirstOrDefault();
                Connection.Table<DanmakuFilterTab>().FirstOrDefault();
                return true;
            }
            catch { }
            return false;
        }
        /// <summary>
        /// 释放对象
        /// </summary>
        public void Dispose()
        {
            try
            {
                Connection.Close();
                Connection.Dispose();
            }
            catch { }
        }
    }
}